<?php


namespace App\Http\Controllers\Backend1;


use App\Services\Base\Interfaces\ConfigServiceInterface;
use App\Services\Base\Services\ConfigService;
use App\Services\Other\Models\WechatPlatform;
use App\Zesow\WechatOpenPlatform;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\Kernel\Support\Collection;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psr\Http\Message\ResponseInterface;


class WechatOpenController extends BaseController
{



    /**
     * @var ConfigService
     */
    protected $configService;

    private $_config = null;

    public function __construct(
        ConfigServiceInterface       $configService
    )
    {
        $this->configService = $configService;

    }




    public function authorizer(Request $request)
    {

        $size = $request->input('size');
        $authorizers = WechatPlatform::query()->orderBy('created_at')->paginate($size);

        return $this->successData(compact('authorizers'));


    }

    /**
     * 模板列表
     * @return JsonResponse
     * @throws InvalidConfigException
     * @throws GuzzleException
     */
    public function miniTemplates()
    {


        $openPlatform = WechatOpenPlatform::getInstance();
        $miniProgramTemplates = $openPlatform->code_template->list();
        return $this->successData(compact('miniProgramTemplates'));

    }

    /**
     * 提交代码
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function commit(Request $request)
    {
        $appId = $request->input('app_id');
        $templateId = $request->input('template_id');
        $extJson = $request->input('ext_json');
        $userVersion = $request->input('user_version');
        $userDesc = $request->input('user_desc');
        $openPlatform = WechatOpenPlatform::getInstance();

        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $openPlatform->miniProgram($appId, $mini->refresh_token)->code->commit($templateId, $extJson, $userVersion, $userDesc);

        return $this->success(__('提交成功'));

    }


    /**
     * 第二步：提交审核
     * @param Request $request
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function submit(Request $request)
    {


        $data = $request->input('data', []);
        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();

        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $openPlatform->miniProgram($appId, $mini->refresh_token)->code->submitAudit($data);


        return $this->success(__('提交成功'));

    }

    /**
     *第三步：发布版本
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function release(Request $request)
    {


        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->firstOrFail();
        $openPlatform->miniProgram($appId, $mini->refresh_token)->code->release();

        return $this->success(__('发布成功'));


    }

    /**
     *撤回版本审核
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function withdraw(Request $request)
    {


        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->firstOrFail();
        $openPlatform->miniProgram($appId, $mini->refresh_token)->code->withdrawAudit();

        return $this->success(__('撤回审核版本成功'));


    }





    /**
     * 获取可设置的类目
     * @return JsonResponse
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function getCategories(Request $request)
    {


        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $categories = $openPlatform->miniProgram($appId, $mini->refresh_token)->setting->getCategories();
        return $this->successData(compact('categories'));

    }

    /**
     * 获取线上版本二维码
     * @param Request $request
     * @return JsonResponse
     */
    public  function getReleaseQrCode(Request $request){


        $scene = $request->input('scene');
        $params = $request->input('data',[]);
        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result =  $openPlatform->miniProgram($appId, $mini->refresh_token)->app_code->getUnlimit($scene,$params);
        $qrcode = 'data:image/png;base64, ' . base64_encode($result);

        return $this->successData(compact('qrcode'));


    }



    /**
     *
     * 获取体验版预览
     * @param Request $request
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function getQrCode(Request $request)
    {


        $path = $request->input('url', '');
        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->code->getQrCode($path);
        $body = 'data:image/png;base64, ' . base64_encode($result);

        return $this->successData(compact('body'));


    }


    /**
     * d
     * @param Request $request
     * @return array|Collection|object|ResponseInterface|string
     * @throws InvalidConfigException
     */
    public function getLatestAuditStatus(Request $request)
    {

        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $data = $openPlatform->miniProgram($appId, $mini->refresh_token)->code->getLatestAuditStatus();
        return $this->successData(compact('data'));

    }


    /**
     * 获取隐私信息
     * @param Request $request
     * @return JsonResponse
     */
    public function getPrivacy(Request $request)
    {


        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->privacy->get();

        return $this->successData(compact('result'));


    }


    /**
     * 提交隐私
     * @param Request $request
     * @return JsonResponse
     */
    public function setPrivacy(Request $request)
    {


        $data = $request->input('data', []);
        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $openPlatform->miniProgram($appId, $mini->refresh_token)->privacy->set($data);


        return $this->success(__('提交隐私成功'));


    }

    /**
     * 版本回滚
     * @param Request $request
     * @return JsonResponse
     */
    public function rollback(Request $request)
    {

        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $openPlatform->miniProgram($appId, $mini->refresh_token)->code->rollbackRelease();
        return $this->success(__('版本发布回滚'));


    }

    /**
     * 推送人脸验证
     * @param Request $request
     * @return JsonResponse
     */
    public function sendIcpVerifyTask(Request $request)
    {


        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->privacy->createIcpVerifyTask();
        return $this->successData(compact('result'));
    }

    /**
     * 验证人脸验证结果
     * @param Request $request
     * @return JsonResponse
     */
    public function getIcpFaceStatus(Request $request)
    {

        $appId = $request->input('app_id');
        $taskId = $request->input('task_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->privacy->getIcpFaceResult($taskId);

        return $this->successData(compact('result'));


    }

    /**
     * 查询备案状态及驳回原因
     * @param Request $request
     * @return JsonResponse
     */
    public function getIcpStatus(Request $request)
    {
        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->privacy->getIcpResult();

        return $this->successData(compact('result'));


    }

    /**
     * 获取小程序版本信息
     * @param Request $request
     * @return JsonResponse
     */
    public function getVersionInfo(Request $request)
    {

        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->with(['company'])->select(['name','head_img','headimg','app_id','refresh_token','company_id'])->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->code->version();

        return $this->successData(compact('result','mini'));
    }

    /**
     * 获取小程序版本信息
     * @param Request $request
     * @return JsonResponse
     */
    public function getPrivacyInfo(Request $request)
    {

        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->security->getCodePrivacyInfo();

        return $this->successData(compact('result'));
    }

    /**
     * 获取小程序版本信息
     * @param Request $request
     * @return JsonResponse
     */
    public function getCodePrivacyInfo(Request $request)
    {

        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->security->getCodePrivacyInfo();

        return $this->successData(compact('result'));
    }

    /**
     * 设置小程序的服务器域名
     * @param Request $request
     * @return JsonResponse
     */
    public function setDomain(Request $request)
    {

        $data = $request->input('data');
        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->domain->modify($data);

        return $this->successData(compact('result'));
    }

    /**
     * 获取小程序的服务器域名
     * @param Request $request
     * @return JsonResponse
     */
    public function getEffectivDomain(Request $request)
    {

        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->domain->getEffectiveServerDomain();

        return $this->successData(compact('result'));
    }


    /**
     * 设置小程序的业务域名
     * @param Request $request
     * @return JsonResponse
     */
    public function setWebDomain(Request $request)
    {

        $domains = $request->input('domains',[]);
        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->domain->setWebviewDomain($domains);

        return $this->successData(compact('result'));
    }

    /**
     * 应用插件
     * @param Request $request
     * @return JsonResponse
     */
    public function  apply(Request $request)
    {
        $appId = $request->input('app_id');
        $openPlatform = WechatOpenPlatform::getInstance();
        $mini = WechatPlatform::query()->where('app_id', $appId)->first();
        $result = $openPlatform->miniProgram($appId, $mini->refresh_token)->plugin->apply($appId);

        return $this->successData(compact('result'));

    }








}